#coding=utf-8
#!/usr/bin/python 
import os
import os.path

file = open("NSUserDefaults+ValueAddedStorage.h")

totleLineCount = 0
spaceLineCount = 0
usefulLineCount = 0
commentLineCount = 0

commentBeginLine = -1
commentEndLine = -1


filename = "/Users/wesleyxiao/Documents/python/ValueAddedService/UI/remindViews/items/RPImageItemView.m"
lines = []
try:
	# http://www.zhihu.com/question/19751023 使用rU读写，统一将换成/n
    f = open(filename, 'rU')
    lines = f.readlines()
except IOError as e:
    print e
else:
    for line in lines:
        # print line
        totleLineCount=totleLineCount+1

        newline = line.strip()
        lineLen = len(newline)
        # print newline
        # 计算空行
        if  lineLen == 0 :
        	spaceLineCount = spaceLineCount+1

        # 计算注释行
        if lineLen >= 2:
	        prefix = newline[0:2]
	        subfix = newline[len(newline)-2:len(newline)]

	        
	        prefixResult =cmp(prefix,"/*")
	        subfixResult =cmp(subfix,"*/")
	        if cmp(prefix,"//")==0:
	        	commentLineCount=commentLineCount+1

	        if cmp(prefix,"/*")==0:
	        	commentBeginLine=totleLineCount;
	        	# print "totleLineCount:%r prefix:%r subfix:%r  前缀cmp:%r" % (totleLineCount,prefix,subfix,prefixResult)
	        if cmp(subfix,"*/")==0:
	        	commentEndLine=totleLineCount;
	        	# print "totleLineCount:%r prefix:%r subfix:%r  后缀cmp:%r" % (totleLineCount,prefix,subfix,subfixResult)
	        
	        if (commentEndLine!=-1 and commentBeginLine!=-1):
	        	commentLineCount=commentEndLine-commentBeginLine+1+commentLineCount
	        	commentEndLine=-1
	        	commentBeginLine=-1
	
finally:
    f.close()

usefulCount = totleLineCount-spaceLineCount-commentLineCount
print "总行数:%r \n 空行:%r\n注释行:%r\n可用代码行数:%r" % (totleLineCount,spaceLineCount,commentLineCount,usefulCount)